package com.dj.mingkong.admin.biz.repository.database.music;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.music.vo.request.MusicQueryVo;
import com.dj.mingkong.admin.biz.util.WrapperUtil;
import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.eo.BaseEo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.music.MusicEo;
import com.dj.mingkong.admin.biz.repository.database.music.mapper.MusicMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.Objects;


/**
 * 音乐Repository
 *
 * @author zenglonghui
 * @date: 2022/5/17 17:22
 */
@Repository
public class MusicRepository extends BaseDatabaseRepository<MusicMapper, MusicEo, QueryVo> {
    public Page<MusicEo> listPageByVo(MusicQueryVo queryVo) {
        final QueryWrapper<MusicEo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Objects.nonNull(queryVo.getId()), BaseEo::getId, queryVo.getId())
                .like(StringUtils.isNotBlank(queryVo.getName()),
                        MusicEo::getName, queryVo.getName())
                .in(StringUtils.isNotBlank(queryVo.getTypeId()), MusicEo::getTypeId,
                        Arrays.stream(queryVo.getTypeId().split(CommonConstants.SPLIT_COMMA)).toList())
                .in(StringUtils.isNotBlank(queryVo.getStyleId()), MusicEo::getStyleId,
                        Arrays.stream(queryVo.getStyleId().split(CommonConstants.SPLIT_COMMA)).toList())
                .eq(StringUtils.isNotBlank(queryVo.getBeFrom()), MusicEo::getBeFrom, queryVo.getBeFrom())
                .eq(StringUtils.isNotBlank(queryVo.getIsExclusive()), MusicEo::getIsExclusive, queryVo.getIsExclusive())
                .eq(StringUtils.isNotBlank(queryVo.getIsRecommend()), MusicEo::getIsRecommend, queryVo.getIsRecommend())
                .eq(StringUtils.isNotBlank(queryVo.getIsBoutique()), MusicEo::getIsBoutique, queryVo.getIsBoutique())
                .eq(StringUtils.isNotBlank(queryVo.getIsVehicle()), MusicEo::getIsVehicle, queryVo.getIsVehicle())
                .in(StringUtils.isNotBlank(queryVo.getLanguageKey()), MusicEo::getLanguageKey,
                        Arrays.stream(queryVo.getLanguageKey().split(CommonConstants.SPLIT_COMMA)).toList())
                .in(StringUtils.isNotBlank(queryVo.getNavMenuKey()), MusicEo::getNavMenuKey,
                        Arrays.stream(queryVo.getNavMenuKey().split(CommonConstants.SPLIT_COMMA)).toList())
                .eq(Objects.nonNull(queryVo.getStatus()), MusicEo::getStatus, queryVo.getStatus())
                .eq(StringUtils.isNotBlank(queryVo.getGenStatus()), MusicEo::getGenStatus, queryVo.getGenStatus());
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }
}
